from openpyxl import load_workbook


class ExcelOperate:

    def __init__(self):
        self.workbook = None
        self.sheet = None

    def load_work(self,filename):
        try:
            self.workbook=load_workbook(filename)
        except Exception as e:
            print(e)

    def get_sheet_by_name(self,sheetname):
        try:
            self.sheet = self.workbook[sheetname]
        except  Exception as e:
            print(e)

    def get_row_nums(self):
        try:
            return self.sheet.max_row
        except Exception as e:
            print(e)

    def get_col_nums(self):
        try:
            return self.sheet.max_column
        except Exception as e:
            print(e)

    def get_row_values(self,row):
        columns = self.sheet.max_column
        row_data = []
        for i in range(1,columns+1):
            cell_value = self.sheet.cell(row=row,column=i).value
            row_data.append(cell_value)
        return row_data

    def get_cell_value(self,row,column):
        cell_value = self.sheet.cell(row=row,column=column).value
        return cell_value

    def write_cell(self,row,column,value,file_name):
        self.sheet.cell(row=row,column=column,value=value)
        self.workbook.save(file_name)

